package com.example.hanjyLearn;

import com.example.hanjyLearn.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/")
public class IndexController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 查询所有汉字列表
     */
    @RequestMapping("/queryAllChineseCharacters")
    public Result<List<Map<String, Object>>> queryAllChineseCharacters(@RequestBody Map<String, String> param) {
        String sql = "SELECT id, character_name AS characterName, learn_date AS date, answer, remark FROM chinese_character";
        List<Map<String, Object>> chineseCharacterList = this.jdbcTemplate.queryForList(sql);
        return Result.success(chineseCharacterList);
    }
    /**
     * 根据日期查询所有汉字列表
     */
    @RequestMapping("/queryAllChineseCharactersGroupByDate")
    public Result<List<Map<String, Object>>> queryAllChineseCharactersGroupByDate(@RequestBody Map<String, String> param) {
        String sql = "SELECT learn_date, GROUP_CONCAT(character_name) AS NAMES FROM chinese_character GROUP BY learn_date;";
        List<Map<String, Object>> characterList = this.jdbcTemplate.queryForList(sql);
        return Result.success(characterList);
    }
    /**
     * 设置该题的答题结果
     **/
    @RequestMapping("/setAnswer")
    public Result<Void> setAnswer(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        String answer = param.get("answer");
        //        操作数据库
        String sql = "UPDATE chinese_character SET answer = ?, answer_time = current_time WHERE id = ?";
        int rows = this.jdbcTemplate.update(sql, answer, id);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("操作失败");
        }
    }

    /**
     * 追加新学的汉字
     * @param param
     * @return
     */
    @RequestMapping("/addNewChineseCharacter")
    public Result<Void> addNewChineseCharacter(@RequestBody Map<String, String> param) {
        String character = param.get("character");
        String date = param.get("date");
        //        操作数据库
        String sql = "INSERT INTO chinese_character (character_name, learn_date) VALUES (?, ?)";
        int rows = this.jdbcTemplate.update(sql, character, date);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("操作失败");
        }
    }

    /**
     * 设置错了，就要删除重新录入
     * @param param
     * @return
     */
    @RequestMapping("/deleteChineseCharacter")
    public Result<Void> deleteChineseCharacter(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        //        操作数据库
        String sql = "delete from chinese_character where id = ?";
        int rows = this.jdbcTemplate.update(sql, id);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("操作失败");
        }
    }
    /**
     * 获取指定范围的指定个数的汉字，用来测验
     */
    @RequestMapping("/querySomeChineseCharacters")
    public Result<List<Map<String, Object>>> querySomeChineseCharacters(@RequestBody Map<String, String> param) {
        String num = param.get("num");
        String range = param.get("range");
        String sql = "SELECT id, character_name AS characterName, learn_date AS date, answer, remark FROM chinese_character WHERE 1=1 ";
        if (range != null && range.equals("01")) {
            sql += "ORDER BY ID ";
        } else if (range != null && range.equals("02")) {
            sql += "ORDER BY ID DESC ";
        } else if (range != null && range.equals("03")) {
            sql += "ORDER BY RAND() ";
        }
        sql += "LIMIT " + num;
        List<Map<String, Object>> chineseCharacterList = this.jdbcTemplate.queryForList(sql);
        return Result.success(chineseCharacterList);
    }

    /**
     * 统计历史测验成总绩
     * @param param
     * @return
     */
    @RequestMapping("/getChineseStatistics")
    public Result<Map<String, Integer>> getChineseStatistics(@RequestBody Map<String, String> param) {
        Integer rightNum = this.jdbcTemplate.queryForObject("SELECT count(1) FROM chinese_character WHERE answer = 'right'", Integer.class);
        Integer rightWrongAllNum = this.jdbcTemplate.queryForObject("SELECT count(1) FROM chinese_character WHERE answer = 'right' OR answer = 'wrong'", Integer.class);
        Map<String, Integer> map = new HashMap<>();
        map.put("rightNum", rightNum);
        map.put("rightWrongAllNum", rightWrongAllNum);
        return Result.success(map);
    }

    /**
     * 获取所有科目的总答题数和总正确数
     * @param param
     * @return
     */
    @RequestMapping("/getTotalStar")
    public Result<Map<String, Integer>> getTotalStar(@RequestBody Map<String, String> param) {
        Integer rightChineseNum = this.jdbcTemplate.queryForObject("SELECT count(1) FROM chinese_character WHERE answer = 'right'", Integer.class);
        Integer allChineseNum = this.jdbcTemplate.queryForObject("SELECT count(1) FROM chinese_character WHERE answer = 'right' OR answer = 'wrong'", Integer.class);
        Integer rightEnglishNum = this.jdbcTemplate.queryForObject("SELECT count(1) FROM english_word WHERE answer = 'right'", Integer.class);
        Integer allEnglishNum = this.jdbcTemplate.queryForObject("SELECT count(1) FROM english_word WHERE answer = 'right' OR answer = 'wrong'", Integer.class);
        Map<String, Integer> map = new HashMap<>();
        map.put("rightChineseNum", rightChineseNum);
        map.put("allChineseNum", allChineseNum);
        map.put("rightEnglishNum", rightEnglishNum);
        map.put("allEnglishNum", allEnglishNum);
        return Result.success(map);
    }
}
